// 文件上传
var express = require('express')
var router = express.Router()

var fs = require('fs')
var path = require('path')
var multer = require('multer')
var upload = multer({ dest: 'tmp_uploads/' })
var upload_config = require('config').get('upload_config')
    // console.log(upload_config);
router.post('/upload', upload.single('file'), (req, res, next) => {
    console.log(req.file);
    var fileEXarray = req.file.originalname.split('.')
    var ext = fileEXarray[fileEXarray.length - 1]
    var targetPath = req.file.destination + req.file.filename + '.' + ext
    console.log(targetPath);
    fs.rename(req.file.path, targetPath, (err) => {
        if (err) return res.ResData('文件上传失败')
    })
    res.ResData("文件上传成功", 200, {
        "temp_path": targetPath,
        "url": upload_config.baseURL + targetPath,
    })
})
module.exports = router